package com.stockz.ui.swing.charting.painter.axis;

import java.awt.Graphics2D;
import java.awt.Rectangle;

import com.stockz.ui.swing.charting.util.MathUtil;
import com.stockz.ui.swing.charting.util.UIChartingConstant;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.charting.view.StockChartPanelSkin;
import com.stockz.ui.swing.charting.view.StockChartPanelState;

public class VolumeAxisPainter{

	public static void paint(Graphics2D g, StockChartPanel stockChartPanel, Rectangle rectangle) {
		StockChartPanelState state = stockChartPanel.getStockChartPanelState();
		StockChartPanelSkin skin = stockChartPanel.getStockChartPanelSkin();
		
		g.setColor(skin.getAxisVolumeColor());
		
		Rectangle r = state.getDrawingRectangle();
		g.drawLine(r.x, r.y, r.x, r.y+r.height);
		
		double min = state.getMinimumVolumeVisible();
		double max = state.getMaximumVolumeVisible();
		double realMin = min;
		double realMax = max;
		
		String letter = null;
		double devisor = 1;
		if(max > 1000000000){
			letter = "B";
			devisor = 1000000000;
		}else if(max > 1000000){
			letter = "M";
			devisor = 1000000;
		}else if(max > 1000){
			letter = "K";
			devisor = 1000;
		}
		
		int div = skin.getAxisDevider();
		
		double increment = (max - min)/div;
		if(min == 0 || max == 0 || increment == 0){
			return;
		}
		/*  */
		double mult = Math.pow(10,Math.floor(Math.log10(increment)));
		double v[] = new double[]{1,2,2.5,5};
		
		for(int d = 1; d < v.length; d++){
			if(increment >= v[d-1]*mult && increment < v[d]*mult){
				increment = v[d-1]*mult;
				break;
			}
		}
		mult = Math.pow(10,Math.floor(Math.log10(min)));
		for(double i = 0; i < 10; i = i + 0.25){
			if(min >= i*mult && min < (i+0.25)*mult){
				min = i*mult;
				break;
			}
		}
		
		/*  */
		int lastTextY = Integer.MAX_VALUE;
		int height = (int)g.getFont().getStringBounds("M", g.getFontRenderContext()).getHeight();
		
		double printedPrice = min;
		int i = 1;
		while(printedPrice <= max){
			
			double val = min+i*increment;
			if(val > max){
				val = max;
			}
			
			g.setColor(skin.getAxisVolumeColor());
			int Y = MathUtil.fitY(rectangle.y, 
					rectangle.y + rectangle.height, 
					realMin, realMax, 
					val,
					state.isSemiLog());
			
			
			if(Y < lastTextY - height -3 && Y >= rectangle.y){
				g.drawLine(rectangle.x, 
						Y,
						rectangle.x + skin.getShortTickLength(), 
						Y);
				g.setColor(skin.getTextColor());
				g.setFont(skin.getSmallFont());
				
				g.drawString(UIChartingConstant.volumeNumberFormat.format(val/devisor)+(letter == null?"":letter), 
						rectangle.x, 
						Y+height+3);
				lastTextY = Y;
			}
			if(val == max){
				printedPrice = val+1;
			}else{
				printedPrice = val;
			}
			
			i++;
		}
	}
	
	public static String formatVolume(double max, double val){
		String letter = null;
		double devisor = 1;
		if(max > 1000000000){
			letter = "B";
			devisor = 1000000000;
		}else if(max > 1000000){
			letter = "M";
			devisor = 1000000;
		}else if(max > 1000){
			letter = "K";
			devisor = 1000;
		}
		return UIChartingConstant.volumeNumberFormat.format(val/devisor)+(letter == null?"":letter);
	}
	
}